﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Microsoft.Forums.OfflineStore;
using System.Diagnostics;

namespace Microsoft.Forums.ForumsClient.Views
{
    /// <summary>
    ///  Class to converts universal time to local time
    /// </summary>
    public class UniversalToLocalTimeConverter : IValueConverter
    {
        /// <summary>
        /// Takes a DateTime in universal format and converts to local format
        /// </summary>
        /// <param name="value">Universal DateTime</param>
        /// <param name="targetType">Must be DateTime</param>
        /// <param name="parameter">not used</param>
        /// <param name="culture">not used</param>
        /// <returns>DateTime in local form or null if value was null </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;
            try
            {
                DateTime universalDateTime = (DateTime)value;
                return universalDateTime.ToLocalTime();
            }
            catch (InvalidCastException e)
            {
                Debug.WriteLine(e);
                throw new ArgumentException("value must be a DateTime in UniversalToLocalTimeConverter");
            }
        }

        /// <summary>
        /// Method Not Implemented. Throws NotSupportedException
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("ConvertBack is not supported.");
        }
    }
}
